<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>构造函数注意问题</title>
</head>
<body>
    <script>
        // 1. 构造函数不写返回 首字母大写
        function Person(name, age) {
            this.name = name;
            this.age = age;
            this.sayHello = function() {
                console.log('hello');
            }
        }

        let obj1 = new Person('zhangsan', 18);
        console.log(obj1);

        // 2. 注意：构造函数的this指向
        function Person2(name, age) {
            this.name = name;
            this.age = age;
            this.sayHello = function() {
                console.log(this);
            }
        }

        let obj2 = new Person2('lisi', 20);
        obj2.sayHello(); // this指向obj2

    </script>
</body>
</html>